﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem.PageView
{
    public partial class OrgForHRControl : UserControl
    {
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        EmpbasicService.OrgForHRModel RoleModel = new EmpbasicService.OrgForHRModel();
        public OrgForHRControl()
        {
            InitializeComponent();
            LoadData();
        }

        void LoadData()
        {
            Client.GetOrgForHRModelCompleted += new EventHandler<EmpbasicService.GetOrgForHRModelCompletedEventArgs>(Client_GetOrgForHRModelCompleted);
            Client.GetOrgForHRModelAsync(0);
        }

        void Client_GetOrgForHRModelCompleted(object sender, EmpbasicService.GetOrgForHRModelCompletedEventArgs e)
        {
            DGRole.ItemsSource = null;
            DGRole.ItemsSource = e.Result;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NewData_Click(object sender, RoutedEventArgs e)
        {
            OrgForHRWindow RMWindow = new OrgForHRWindow(0, true);
            RMWindow.Closed += new EventHandler(RMWindow_Closed);
            RMWindow.Show();
        }
        void RMWindow_Closed(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            LoadData();
        }

        private void EditData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as EmpbasicService.OrgForHRModel;

            //throw new NotImplementedException();
            if (seljw != null)
            {
                OrgForHRWindow RMWindow = new OrgForHRWindow(Convert.ToInt32(seljw.ID), false);
                RMWindow.Closed += new EventHandler(RMWindow_Closed);
                RMWindow.Show();

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要编辑的部门！"); 
        }

        private void DeleteData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as EmpbasicService.OrgForHRModel;

            //throw new NotImplementedException();
            if (seljw != null)
            {
                if (MessageBox.Show("确定删除吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    Client.DeteleOrgForHRModelCompleted -= new EventHandler<EmpbasicService.DeteleOrgForHRModelCompletedEventArgs>(Client_DeteleOrgForHRModelCompleted);
                    Client.DeteleOrgForHRModelCompleted += new EventHandler<EmpbasicService.DeteleOrgForHRModelCompletedEventArgs>(Client_DeteleOrgForHRModelCompleted);
                    Client.DeteleOrgForHRModelAsync(Convert.ToInt32(seljw.ID));
                }

            }
        }

        void Client_DeteleOrgForHRModelCompleted(object sender, EmpbasicService.DeteleOrgForHRModelCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("删除成功！");
            LoadData();
        }
    }
}
